package io.kiki.sba.registry.client.provider;

import io.kiki.sba.registry.client.api.ClientConfig;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class SyncServerListThread extends Thread {
    private static final Logger logger = LoggerFactory.getLogger(SyncServerListThread.class);
    private final ServerManagerImpl serverManagerImpl;
    private ClientConfig clientConfig;


    public SyncServerListThread(ServerManagerImpl serverManagerImpl, ClientConfig clientConfig) {
        this.serverManagerImpl = serverManagerImpl;
        this.setName("SyncServerListThread");
        this.setDaemon(true);
        this.clientConfig = clientConfig;
    }


    @Override
    public void run() {
        int retryInterval;
        while (true) {
            try {
                retryInterval = Math.max(ServerManagerImpl.MIN_RETRY_INTERVAL, clientConfig.getSyncConfigRetryInterval());
                Thread.sleep(retryInterval);

                serverManagerImpl.syncServerList();
            } catch (Throwable e) {
                logger.error("[serverManager] sync server list task error", e);
            }
        }
    }
}
